<template>
	<div>
		<div class="content">
			<image class="logo" src="https://czsc.oss-cn-beijing.aliyuncs.com/mini/expert_sd.png"></image>
			<p class='titleText'>申请获取以下权限</p>
			<p class='contentText'>获得你的公开信息(昵称,头像,手机等)</p>
		</div>
		<div style="margin-top: 20px;">
			<view>
				<van-button type="primary" size="large" @click="login">一键授权</van-button>
			</view>
		</div>
	</div>
</template>

<script>
	import {
		// mapState,  
		mapMutations
	} from 'vuex';
	var that;
	export default {
		data() {
			return {
				title: 'Hello',
				code: ''
			}
		},
		onLoad: function() {
			that = this;
			uni.login({
				success: (res) => {
					console.log(res)
					if (res.errMsg == "login:ok") {
						this.code = res.code;
					} else {
						uni.showToast({
							title: '系统异常，请联系管理员'
						})
					}
				},
				error: (res) => {
					console.log(res)
				}
			})
		},
		methods: {
			...mapMutations(['login']),
			//微信授权登录
			login() {
				
				// const myDate = new Date();
				// uni.setStorageSync('getday', myDate);
				// uni.reLaunch({
				// 	url: '/pages/menu/index'
				// });
				// return
				uni.getUserProfile({
					desc: '用于登录',
					success: (res) => {
						uni.setStorageSync('nickname', res.userInfo.nickName);
						uni.setStorageSync('avatarurl', res.userInfo.avatarUrl);
						uni.request({
							header: {
								'content-type': 'application/x-www-form-urlencoded'
							},
							url: 'https://www.cr12cz.cn/czscsms/getopenid/openid', //你的接口地址
							//url: 'http://localhost:8086/getopenid/openid',
							method: 'POST', //接口类型 
							data: {
								code: that.code
							}, //接口需要的数据
							success: function(res) {
								console.log(res);
								if (res.data.openid != null) {
									const myDate = new Date();
									uni.setStorageSync('getday', myDate);
									uni.showToast({
										title: '授权成功',
										duration: 2000
									});
									uni.setStorageSync('openid', res.data.openid);
									uni.reLaunch({
										url: '/pages/menu/index'
									});
								} else {
									uni.showToast({
										title: '请不要重复授权！',
										mask: true,
										icon: 'none'
									})
								}
								if (res.data == null) {
									uni.showToast({
										title: '授权失败！',
										mask: true,
										icon: 'none'
									})
								}
							}
						})
					},
					fail: (res1) => {
						console.log(res1)
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.titleText {

		margin-left: 25px;

		margin-top: 25px;

		margin-bottom: 10px;

		font-size: 14px;

		color: #020e0f;

		text-align: center;

	}

	.contentText {

		margin-left: 25px;

		margin-top: 0px;

		margin-bottom: 0px;

		font-size: 14px;

		color: #666;

		text-align: center;

	}
</style>

</style>

</style>
